package com.sm.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sm.model.Goods;
import com.sm.model.ShopCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.sm.service.GoodsService;
import com.sm.service.ShopCarService;
import com.sm.util.Msg;
import com.sm.vo.ShopCarGoodsVO;
import com.sm.vo.ShopCarNumberVO;

/**
 *	购物车controller
 * @author 60915
 */
@Controller
@RequestMapping("/shopCar")
public class ShopCarController {


	@Autowired
	private ShopCarService shopCarService;
	@Autowired
	private GoodsService goodsService;

	/**
	 *	将商品加入到购物车中
	 * @param userId		用户id
	 * @param goodsId		商品id
	 * @param format		商品规格
	 * @param goodsNumber	商品数量
	 * @return
	 */
	@RequestMapping("/addShopCar")
	@ResponseBody
	public Msg addShopCar(Integer userId, Integer goodsId, String format, Integer goodsNumber) {
		if(userId == null || goodsId == null || goodsNumber == null) {
			return Msg.fail().add("msg","数据异常，请重试");
		}

		//通过用户id，商品id，和商品规格查询购物是否存在这个商品
		ShopCar shopCar = shopCarService.selectShopCarIfBeing(userId, goodsId, format);
		if(shopCar == null ) {
			//查询商品详情
			Goods goods = goodsService.selectByPrimaryKey(goodsId);//通过商品id查询商品
			ShopCar shopCarNew = new ShopCar();//创建一个购物车实体类
			shopCarNew.setUserId(userId);//设置用户id
			shopCarNew.setGoodsId(goodsId);//设置商品id
			shopCarNew.setGoodsNumber(goodsNumber);//设置商品数量
			shopCarNew.setGoodsMarketPrice(goods.getGoodsMarketPrice());//设置市场价格
			shopCarNew.setGoodsDiscountPrice(goods.getGoodsDiscountPrice());//设置优惠价格
			shopCarNew.setGoodsVipPrice(goods.getGoodsVipPrice());//设置vip价格
			shopCarNew.setGoodsName(goods.getGoodsName());//设置商品名称
			shopCarNew.setCreateTime(new Date());//设置加入购物车时间
			if(format != null && format != "") {
				shopCarNew.setFormat(format);//设置商品规格
			}

			int count = shopCarService.insertSelective(shopCarNew);//插入购物车数据。
			if(count == 1) {
				return Msg.success().add("msg", "添加成功");
			}else {
				return Msg.fail().add("msg", "添加失败");
			}

		}else {
			int number = shopCar.getGoodsNumber();//查询之前的购物车中的商品的数量
			shopCar.setGoodsNumber(goodsNumber+number);//设置购物车中的商品的新的数量。

			//更新购物车中的数量
			int count = shopCarService.updateByPrimaryKeySelective(shopCar);
			if(count == 1) {
				return Msg.success().add("msg", "添加成功");
			}else {
				return Msg.fail().add("msg", "添加失败");
			}
		}
	}

	/**
	 *	展示购物车中的商品
	 * @param userId	用户id
	 * @return
	 */
	@RequestMapping("/showShopCar")
	@ResponseBody
	public Msg showShopCar(Integer userId) {

		List<ShopCar> shopCars = shopCarService.selectShopCarByUserId(userId);//查询购物车的数据
		//创建一个集合存放购物车与商品的实体类
		List<ShopCarGoodsVO> shopCarGoodsVOs = new ArrayList<ShopCarGoodsVO>();

		if(shopCars != null && shopCars.size() != 0) {
			for (ShopCar shopCar : shopCars) {//遍历购物车
				Goods goods = goodsService.selectByPrimaryKey(shopCar.getGoodsId());//通过商品id查询商品信息
				ShopCarGoodsVO shopCarGoodsVO = new ShopCarGoodsVO(goods,shopCar);//创建购物车与商品的实体类
				shopCarGoodsVOs.add(shopCarGoodsVO);//将购物车与商品实体类存入到集合中
			}
		}
		return Msg.success().add("shopCarGoodsVOs", shopCarGoodsVOs);
	}

	/**
	 *	删除购物车
	 * @param shopCarIds		购物车id
	 * @return
	 */
	@RequestMapping("/deleteShopCar")
	@ResponseBody
	public Msg deleteShopCar(Integer[] shopCarIds) {
		if(shopCarIds == null || shopCarIds.length == 0) {
			return Msg.fail().add("msg", "数据异常请重试");
		}

		for (Integer shopCarId : shopCarIds) {
			shopCarService.deleteByPrimaryKey(shopCarId);
		}

		return Msg.success().add("msg", "删除成功");
	}

	/**
	 *	修改用户的购物车数量
	 * @param userId	用id
	 * @param shopCars	购物车id和商品数量集合
	 * @return
	 */
	@RequestMapping("/updateShopCarNumber")
	@ResponseBody
	public Msg updateShopCarNumber(Integer userId, String shopCars) {
		System.out.println("userId========="+userId);
		System.out.println("shopCars========="+shopCars);

		if(shopCars == null || shopCars == "") {
			return Msg.fail().add("msg", "没有要修改的购物车");
		}

		//获取前台传递过来的购物车以及其数量的集合
		List<ShopCarNumberVO> list=JSONArray.parseArray(shopCars,ShopCarNumberVO.class);
		for (ShopCarNumberVO shopCarNumberVO : list) {//遍历这个集合
			//通过用户id和购物车id查询购物车数据
			ShopCar shopCar = shopCarService.selectShopCarByGoodsId(userId, shopCarNumberVO.getShopCarId());
			shopCar.setGoodsNumber(shopCarNumberVO.getGoodsNumber());//设置购物车中商品的数量
			shopCarService.updateByPrimaryKeySelective(shopCar);//更新数据库中购物车的商品数量
		}

		return Msg.success().add("msg", "修改成功");
	}

//	/**
//	 *	清空购物车
//	 * @param userId	用户id
//	 * @return
//	 */
//	public Msg cleanShopCar(Integer userId) {
//		if(userId == null) {
//			return Msg.fail().add("msg", "数据异常请重试");
//		}
//
//		//清空购物车
//		shopCarService.cleanShopCar(userId);
//
//		return Msg.success().add("msg", "清除成功");
//	}

//	/**
//	 *	购物车中的加减号
//	 * @param shopCarId		购物车id
//	 * @param addAndSub		加减符号(1:加号。-1:减号)
//	 * @return
//	 */
//	@RequestMapping("/updateShopCarNumber")
//	@ResponseBody
//	public Msg updateShopCarNumber(Integer shopCarId, Integer addAndSub) {
//
//		ShopCar shopCar = shopCarService.selectByPrimaryKey(shopCarId);//通过购物车id查询购物车的数据
//		int number = shopCar.getGoodsNumber();//获取该购物车中的该商品的数量。
//
//		shopCar.setGoodsNumber(number+addAndSub);//进行购物车中商品数量的加减。
//		int count = shopCarService.updateByPrimaryKeySelective(shopCar);//更新购物车商品数值。
//		if(count == 1) {
//			return Msg.success().add("msg", "修改成功");
//		}else {
//			return Msg.fail().add("msg", "修改失败");
//		}
//	}






}






























